#include "firfilter.h"

FIRFilter::FIRFilter(int period, QObject *parent) : QObject(parent)
{
    AD_Period = period;
    inputSignals.clear();
    outputValue = 0;
    fOutputValue = 0;
}

FIRFilter::FIRFilter(QObject *parent) : QObject(parent)
{
    AD_Period = 1;
    inputSignals.clear();
    outputValue = 0;
    fOutputValue = 0;
}

void FIRFilter::setPeriod(int p)
{
    AD_Period = p;
}

void FIRFilter::setInput(double signal) // signal: mm
{
    inputSignals.append(signal * 1000); // Signals: um
    double Vi_k_m = inputSignals.size() < AD_Period + 1 ?
                0 : inputSignals.at(inputSignals.size() - AD_Period - 1);
    outputValue = 1.0 / AD_Period * (inputSignals.last() - Vi_k_m)
            + outputValue;
//    qDebug("AD_Period: %d", AD_Period);
    fOutputValue = outputValue / 1000.0;
}

const double & FIRFilter::getOutput() const
{
    return fOutputValue;
}

void FIRFilter::reset()
{
    inputSignals.clear();
    outputValue = 0;
}
